package com.cw.carwashemployee.controller;

import com.cw.carwashemployee.domain.Employee;
import com.cw.carwashemployee.service.EmployeeService;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询员工列表
     * @param employee
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(Employee employee) {
        startPage();
        List<Employee> employees = employeeService.selectemployeeList(employee);
        return getDataTable(employees);
    }

    /**
     * 查询员工详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Employee detail(@PathVariable(value = "id") Long id) {
        return employeeService.selectemployeeById(id);
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping("/add")
    public int add(@RequestBody Employee employee) {
        return employeeService.insertemployee(employee);
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    @PutMapping("/update")
    public int update(@RequestBody Employee employee) {
        return employeeService.updateemployee(employee);
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public int delete(@PathVariable Long id) {
        return employeeService.deleteemployeeById(id);
    }

    /**
     * 批量删除员工
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    public int delete(@PathVariable Long[] ids) {
        return employeeService.deleteemployeeByIds(ids);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, Employee employee) {
        List<Employee> employees = employeeService.selectemployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.exportExcel(response, employees, "员工数据");
    }
}
